<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\AdminApi\Controller;

use App\AdminApi\BasicsController;
use App\AdminApi\Modular\Administer\Search\LoginLogSearch;
use App\AdminApi\Modular\Administer\Search\LogSearch;
use App\Common\Extend\Guard\Auth;
use Core\HyperfUnit\Response;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Psr\Http\Message\ResponseInterface;

#[
    Controller(prefix: '/admin-api/console', server: self::SERVER),
    Auth(guard: self::GUARD, sign: 'skip', app: self::APP)
]
class ConsoleController extends BasicsController
{
    #[
        GetMapping('login-log'),
        Auth(label: ['登录日志'], sign: 'skip', logger: false, describe: '登录日志', view: '/console/login-log/index')
    ]
    public function LoginLog(): ResponseInterface
    {
        $search = new LoginLogSearch([
            'where' => [
                'user_id' => $this->user()->getId(),
                ...$this->request->inputs(['os', 'ip', 'browser', 'time']),
            ],
            'size' => $this->pageSize(),
            'page' => (int) $this->request->input('page', 1),
            'order' => $this->orderBy('id DESC'),
            'column' => ['id', 'os', 'browser', 'ip', 'ascription', 'type', 'time'],
        ]);
        return Response::Api()->success(data: $search->search());
    }

    #[
        GetMapping('log'),
        Auth(label: ['操作日志'], sign: 'skip', logger: false, describe: '操作日志', view: '/console/log/index')
    ]
    public function log(): ResponseInterface
    {
        $search = new LogSearch([
            'where' => [
                'user_id' => $this->user()->getId(),
                ...$this->request->inputs(['name', 'ip', 'url', 'time', 'os']),
            ],
            'size' => $this->pageSize(),
            'page' => (int) $this->request->input('page', 1),
            'order' => $this->orderBy('id DESC'),
            'column' => ['id', 'name', 'status', 'url', 'os', 'browser', 'ip', 'ascription', 'time'],
        ]);
        return Response::Api()->success(data: $search->search());
    }
}
